package com.vlong.town.controller.admin.goods;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.base.TreeData;
import com.vlong.town.entity.goods.GoodsInfo;
import com.vlong.town.entity.goodsType.GoodsType;
import com.vlong.town.service.goods.GoodsInfoService;
import com.vlong.town.service.goodsType.GoodsTypeService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 本地特产信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/goods")
public class GoodsInfoController extends BaseController {

    // Fields

    @Autowired
    private GoodsInfoService goodsInfoService;// 本地特产信息业务实现类
    @Autowired
    private GoodsTypeService  GoodsTypeService;//特产类型业务接口

    /**
     * 进入本地特产信息列表页面
     * @return goods/list_goods
     */
    @RequestMapping("/list")
    public String gotoGoodsInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<GoodsInfo> goodsInfoList = this.goodsInfoService.queryList(null, title, content, desc,null);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<GoodsInfo>(goodsInfoList);
        PageTag<GoodsInfo> pt = new PageTag<GoodsInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(GoodsInfo goodsInfo : goodsInfoList) {
            goodsInfo.setContent(DataUtil.delHTMLTag(goodsInfo.getContent(), 0));
        }
        request.setAttribute("goodsInfoList", goodsInfoList);
        return "admin/goods/list_goods";
    }

    /**
     * 进入本地特产信息添加页面
     * @return goods/add_goods
     */
    @GetMapping("/addGoods")
    public String gotoGoodsInofAddPage() {
    	/*List<GoodsType> goodsTypeList = GoodsTypeService.queryList(null, null, null, 1, null, null, TownConstant.DEL_FLAG_0, null, null, null, null);
    	List<TreeData> treeDataList = new ArrayList<TreeData>();
		treeDataList.add(new TreeData("0", null, "特产分类[选择特产所属分类]", false, true));
		for(GoodsType goodsType : goodsTypeList){
			treeDataList.add(new TreeData(goodsType.getTypeId().toString(),goodsType.getTypePid().toString(), goodsType.getTypeName()+ "[" + (goodsType.getTypeDesc() == null || "".equals(goodsType.getTypeDesc().trim()) ? "—"  : goodsType.getTypeDesc()) + "]", false, true));
		}
		request.setAttribute("treeData",JSONArray.toJSONString(treeDataList));*/
    	return "admin/goods/add_goods";
    }

    /**
     * 进入本地特产信息修改页面
     * @return goods/edit_goods
     */
    @GetMapping("/editGoods/{goodsId}")
    public String gotoGoodsInofEditPage(@PathVariable(required=true, value="goodsId") Long goodsId) {
    	GoodsInfo goodsInfo = this.goodsInfoService.queryById(goodsId);
/*        List<GoodsType> goodsTypeList = GoodsTypeService.queryList(null, null, null, 1, null, null, TownConstant.DEL_FLAG_0, null, null, null, null);
        List<TreeData> treeDataList = new ArrayList<TreeData>();
		treeDataList.add(new TreeData("0", null, "特产分类[选择特产所属分类]", false, true));
        for (GoodsType goodsType : goodsTypeList) {
        	boolean isChecked = false;
			if(goodsInfo.getGoodsTypeId().longValue()==goodsType.getTypeId()){
				isChecked = true;
			}
			treeDataList.add(new TreeData(goodsType.getTypeId().toString(),goodsType.getTypePid().toString(), goodsType.getTypeName()+ "[" + (goodsType.getTypeDesc() == null || "".equals(goodsType.getTypeDesc().trim()) ? "—"  : goodsType.getTypeDesc()) + "]", isChecked, true));
		}
        request.setAttribute("treeData",JSONArray.toJSONString(treeDataList));
*/        request.setAttribute("goodsInfo", goodsInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + goodsInfo.getThumbnail());
        return "admin/goods/edit_goods";
    }

    /**
     * 添加本地特产信息
     * @param title 本地特产标题
     * @param content 本地特产内容
     * @param desc 本地特产简介
     * @param picture 缩略图
     * @param feature 本地特产特征
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddGoods")
    public DataMessage addGoods(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="goodsTypeId", required=true) String goodsTypeId
    		) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("本地特产名称不合法");
            return dm;
        }
        this.goodsInfoService.add(1L, 0, title, content, desc, picture, feature,Long.parseLong(goodsTypeId));
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("本地特产添加成功");
        return dm;
    }

    /**
     * 修改本地特产信息
     * @param title 本地特产标题
     * @param content 本地特产内容
     * @param desc 本地特产简介
     * @param picture 缩略图
     * @param feature 本地特产特征
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditGoods")
    public DataMessage editGoods(
            @RequestParam(name="goodsId", required=true) Long goodsId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="goodsTypeId", required=true) String goodsTypeId) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("本地特产名称不合法");
            return dm;
        }
        this.goodsInfoService.update(goodsId, title, content, desc, picture, feature,Long.parseLong(goodsTypeId));
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("本地特产信息修改成功");
        return dm;
    }

    /**
     * 删除本地特产
     * @param goodsId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="goodsId") Long goodsId) {
        DataMessage dm = new DataMessage();
        if(goodsId != null && goodsId > 0L) {
            this.goodsInfoService.delete(goodsId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("本地特产信息删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除本地特产
     * @param goodsIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteGoods(@RequestParam(required=true, value="goodsIds") String goodsIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(goodsIds, 1, 255)) {
            String[] goodsIdArray = goodsIds.split(",");
            for(String goodsId : goodsIdArray) {
                this.goodsInfoService.delete(Long.parseLong(goodsId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("本 地特产信息删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
